//
//  DealMessageVC.m
//  HengYouCai
//
//  Created by ycgwl on 2017/5/3.
//  Copyright © 2017年 lwj. All rights reserved.
//

#import "DealMessageVC.h"
#import "DealMessageModel.h"
@interface DealMessageVC ()
{
    NSInteger _pageNo;
}
/**
 四个小图标
 */
@property (nonatomic, retain) NSArray *imgArr;

/**
 数据源
 */
@property (nonatomic, retain) NSMutableArray *totalArr;
@end

@implementation DealMessageVC
- (void)dealloc{
    NSLog(@"===DealMessageVC.h====");
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
   
    self.navigationItem.title = @"消息中心";

    self.tableView.backgroundColor = Normal_BackColor;
    self.tableView.estimatedRowHeight = 100;
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    _pageNo = 1;

    [self.view showHUDLoading];
    [self startNetworking];
    [self startRefresh];
    self.tableView.mj_footer.hidden = YES;

}
#pragma mark- 刷新
- (void)startRefresh
{
    @weakify(self)
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        @strongify(self);
        self -> _pageNo = 1;
        
        [self_weak_ startNetworking];
        
    }];
    
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        @strongify(self);
        self -> _pageNo++;

        [self_weak_ startNetworking];
    }];
    
    [self.tableView.mj_header setAutomaticallyChangeAlpha:YES];

}
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
}
#pragma tableviewDatasource
//- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
//{
//    return 75;
//}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _totalArr.count;
}
#pragma tableviewDelegate

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"dealMessageCellID"];
    
    DealMessageModel *model = _totalArr[indexPath.row];
//    UIImageView *img = [cell viewWithTag:1001];
//    img.image = [UIImage imageNamed:self.imgArr[indexPath.row%4]];
    UILabel *mesgLab = [cell viewWithTag:1002];
//    NSArray *arr = @[@"",@"君不见黄河之水天上来，奔流到海不复回",
//                     @"人生得意须尽欢，莫使金樽空对月。天生我材必有用，千金散尽还复来。",
//                     @"葡萄美酒夜光杯，欲饮琵琶马上催。醉卧沙场君莫笑，古来征战几人回",
//                     @"三十功名尘与土，八千里路云和月。莫等闲，白了少年头，空悲切。臣子恨，何时灭。驾长车，踏破贺兰山缺。壮志饥餐胡虏肉，笑谈渴饮匈奴血。待从头、收拾旧山河，朝天阙",
//                     @"隔座送钩春酒暖，分曹射覆蜡灯红 杜诗韩集愁来读，似倩麻姑痒处抓 读书破万卷"];
//    mesgLab.text = arr[arc4random()%6];
    mesgLab.text = model.content;
    UILabel *timteLab = [cell viewWithTag:1003];
    timteLab.text = model.date;
    return cell;
}
#pragma 懒加载
- (NSArray *)imgArr
{
    if (!_imgArr) {
        _imgArr = @[@"chong",@"tou",@"ti",@"hui"];
    }
    return _imgArr;
}
- (NSMutableArray *)totalArr
{
    if (!_totalArr) {
        _totalArr = [NSMutableArray array];
    }
    return _totalArr;
}
- (void)startNetworking
{
    WLGetWeakSelf(weakSelf, self);
    [HYCNetService accountSysAlertMessageWithDict:@{@"pageNo":[NSString stringWithFormat:@"%ld",_pageNo],@"pageSize":@"10"} onSuccess:^(NSDictionary *dictResult) {
        //
        [weakSelf.view hideHUDLoading:0];
        NSArray *lists = dictResult[@"smgList"];
        
    
        NSLog(@"===11111===");
        
        [weakSelf.tableView.mj_header endRefreshing];
        [weakSelf.tableView.mj_footer endRefreshing];

        if (_pageNo==1) {
            [_totalArr removeAllObjects];
         
        }
        weakSelf.tableView.mj_footer.hidden = lists.count<10;
        [weakSelf.totalArr addObjectsFromArray:[DealMessageModel mj_objectArrayWithKeyValuesArray:lists]];
        NSLog(@"===22222===");

        [weakSelf.tableView reloadData];

        NSLog(@"===3333===");


    } andFailed:^(NSInteger code, NSString *errorMsg) {
        //
        [weakSelf.view showHUDFailTips:errorMsg hideDelay:kHUDHideDelay];
        [weakSelf.tableView.mj_header endRefreshing];
        [weakSelf.tableView.mj_footer endRefreshing];

    }];
}

@end
